<?php 
/**
* 
*/
class InitController{
	
	
	function tableCreate(){
		
		include(ROOT.'view/admin/tpl/table_create.html');

	}

	function teacherAdd(){
		include(ROOT.'view/admin/tpl/teacher_add.html');


	}

	function teacherAddAction(){
		print_r($_POST);

		$model=new TeacherModel();
		
		$data=array();
		
	//先对POST上来的数据自动过滤
		$data=$model->cut($_POST);

	//print_r($data);exit;

	//再对过滤后的$data自动补充
		$data=$model->fillFields($data);
	//print_r($data);exit;
			
		$message = new MessageTool();
		if(!$model->add($data)){//用户表插入成功
			$id=$model->insertId();
			$msg = '添加教师失败';
			$message->prompt($msg,"./index.php?c=init&a=teacherAdd");
			   exit;
			//print_r($data1);
		  

		  
		}else{
			
		   $msg = '添加教师成功';
		   $message->prompt($msg,"./index.php?c=init&a=teacherAdd");
		   exit;
		  
		}

	
}

	
	function teacherList(){
		$model=new RoleModel;
		$data=$model->getRoleExtAdmin();

		include(ROOT.'view/admin/tpl/teacher_list.html');
	}

	function teacherDel(){


	}

	function courseAdd(){
		include(ROOT.'view/admin/tpl/course_add.html');
	}


    function courseAddAction(){
		//print_r($_POST);

		$model=new CourseModel();
		
		$data=array();
		
	//先对POST上来的数据自动过滤
		$data=$model->cut($_POST);

	//print_r($data);exit;

	//再对过滤后的$data自动补充
		$data=$model->fillFields($data);
	//print_r($data);exit;
			
		$message = new MessageTool();
		if(!$model->add($data)){//用户表插入成功
			$id=$model->insertId();
			$msg = '添加课程失败';
			$message->prompt($msg,"./index.php?c=init&a=teacherAdd");
			   exit;
			//print_r($data1);
		  
		}else{
			
		   $msg = '添加课程成功';
		   $message->prompt($msg,"./index.php?c=init&a=teacherAdd");
		   exit;
		  
		}

	
}


	function courseList(){
		$model=new CourseModel;
		$data=$model->findAll();

		include(ROOT.'view/admin/tpl/course_list.html');
	}

	function courseDel(){

	}

    function setCourse(){

    	$rbac = new RbacModel();
		
		$_SESSION['uname']=isset($_SESSION['uname'])?$_SESSION['uname']:" ";
		$role = $rbac->selectRole($_SESSION['uname']);
        $role['name'] == "Admin";

    	$cs=array();
    	$bj=array();
    	$access = array();
    	$role_id=$_GET['role_id']+0;
    	$model1=new RoleModel;
    	$row=$model1->findOne($role_id);
    	$name=$row['name'];

        $course=new ClassModel;
        $nodes=new NodeModel;
    	//找权限(班级),将班级里的课程取出
    	$model2=new AccessModel;
    	$class=$model2->getRoleAccess($role_id);

    	//var_dump($class);
    	$tc = new TeacherCourseModel;
  
    	foreach ($class as $v) {
			$rel=$nodes->findOne($v['node_id']);
	        $bj[]=$rel;
	        
	        $km=$course->findClass($rel['name']);
	        $c = $tc->getCourse($role_id,$v['name']);
	        foreach ($c as $v1) {
	        	$access[$v['name']][] = $v1['course'];
	        }
	        $cs[]=$km;
    	}
    	$i=0;
    	include(ROOT.'view/admin/tpl/course_set.html');

    }
    
  function setCourseAction(){
   	$message = new MessageTool;
   	$tc = new TeacherCourseModel;
   	$tc->deleteCourse($_POST["role_id"]);
   	$data = array();
   	if(isset($_POST["kecheng"])){
	   	foreach ($_POST["kecheng"] as $v) {
	   		$temp = explode("_", $v);
	   		$data[] =array(
	   			"role_id"=>$_POST["role_id"],
	   			"class"=>$temp[0],
	   			"course"=>$temp[1],
	   			"cno"=>$temp[2]
	   			) ;
	   	}

	   	if($tc->addAll($data)){
	   		$msg = "配置课程成功";
	   		$message->prompt($msg,"./index.php?c=init&a=teacherList");
	   		exit;
	   	}else{
	   		$msg = "配置课程失败！";
	   		$message->prompt($msg,"./index.php?c=init&a=teacherList");
	   		exit;
	   	}
    }else{
    	$msg = "配置课程失败！";
   		$message->prompt($msg,"./index.php?c=init&a=teacherList");
   		exit;
    }
   	//var_dump($data);
   
   	
   }
}
 ?>